<?php


namespace App\Http\Controllers;


use App\Services\UserService;
use App\Transformer\UserTransformer;
use Dingo\Api\Http\Request;

class UserController extends Controller
{

    /**
     * 用户列表
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function search(Request $request)
    {
        return $this->response->paginator(UserService::search($request), new UserTransformer());
    }

    /**
     * 选择用户列表树
     * @author thanatos thanatos915@163.com
     */
    public function tree()
    {
        return $this->resArray(UserService::tree());
    }

    /**
     * 用户详情
     * @param $id
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function show($id)
    {
        return $this->response->item(UserService::show($id), new UserTransformer());
    }

    /**
     * 创建用户
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function create(Request $request)
    {
        can('user_create');
        return $this->response->item(UserService::createAndUpdate($request, 0), new UserTransformer());
    }

    /**
     * 保存用户
     * @param Request $request
     * @param $id
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function update(Request $request, $id)
    {
        can('user_update');
        return $this->response->item(UserService::createAndUpdate($request, $id), new UserTransformer());
    }

    /**
     * 冻结用户
     * @param Request $request
     * @author thanatos thanatos915@163.com
     */
    public function freeze(Request $request)
    {
        can('user_freeze');
        UserService::freeze($request);
        $this->response->noContent();
    }

    /**
     * 删除用户
     * @param $id
     * @throws \Exception
     * @author thanatos thanatos915@163.com
     */
    public function delete($id)
    {
        can('user_delete');
        UserService::delete($id);
        $this->response->noContent();
    }

}
